package com.example.onlineeducation.ui.main


import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import com.example.onlineeducation.App
import com.example.onlineeducation.R
import com.example.onlineeducation.databinding.MainMineFragmentBinding
import com.example.onlineeducation.base.BaseFragment
import com.example.onlineeducation.data.db.datastore.UserInfoDataStoreKey
import com.example.onlineeducation.ui.auth.LoginActivity
import com.example.onlineeducation.ui.mine.*
import com.example.onlineeducation.ui.offline.OfflineStudyListAc
import com.example.onlineeducation.utils.YcSpannable
import com.example.onlineeducation.viewmodel.MainHomeVM
import com.example.onlineeducation.viewmodel.MainMineVM
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.yc.jetpacklib.data.datastore.YcDataStore.ycDataStore
import com.yc.jetpacklib.init.YcJetpack
import com.yc.jetpacklib.net.doFail
import com.yc.jetpacklib.net.doSuccess
import com.yc.jetpacklib.utils.YcUI
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map


class MainMineFragment : BaseFragment<MainMineFragmentBinding>(MainMineFragmentBinding::inflate) {

    override fun initImmersionBar() {
        ImmersionBar.with(this)
            .statusBarColor(R.color.transparent)
            .statusBarDarkFont(false)
            .hideBar(BarHide.FLAG_SHOW_BAR)
            .fitsSystemWindows(false)
            .init()
    }

    private val mViewModel: MainMineVM by ycViewModels()
    private var mProId: String = ""
    @SuppressLint("SetTextI18n")
    override fun MainMineFragmentBinding.initView(view: View, savedInstanceState: Bundle?) {
        App.mInstant.mViewModel.mProInfoEntity.observe {
            if (it != null) {
                tvProName.text = it.proName
                mProId = it.proId
                srl.autoRefresh()
            } else
                tvProName.text = "-"
        }
        launch {
            YcJetpack.mInstance.mApplication.ycDataStore.data.map {
                tvUserName.text = it[UserInfoDataStoreKey.KEY_USER_NAME] ?: "-"
            }.first()
        }
        srl.setOnRefreshListener {
            mViewModel.initData(mProId)
        }
        vStudyTotal.setOnClickListener {
            StatisticsStudyTotalAc.newInstance(this@MainMineFragment.requireContext())
        }
        vStudyToday.setOnClickListener {
            StatisticsStudyTodayAc.newInstance(this@MainMineFragment.requireContext())
        }

        vPassAndAllExams.setOnClickListener {
            UnpassExamListActivity.newInstance(this@MainMineFragment.requireContext())
        }
        vParticipated.setOnClickListener {
            OfflineStudyListAc.newInstance(this@MainMineFragment.requireContext(), false)
        }
        clOfflineActivity.setOnClickListener {
            OfflineStudyListAc.newInstance(this@MainMineFragment.requireContext())
        }
        clIntegralActivity.setOnClickListener {
            ScoreRecordActivity.newInstance(requireActivity())
        }
        clExamRecord.setOnClickListener {
            ExamRecordActivity.newInstance(requireActivity())
        }
        clLearningByMistake.setOnClickListener {
            WrongTopicLearningActivity.newInstance(requireActivity())
        }
//        clCertificate.setOnClickListener {
//            CertificateListActivity.newInstance(requireActivity())
//        }
        clLogout.setOnClickListener {
            mViewModel.logout()
        }
        tvSelectPro.setOnClickListener {
            ProListActivity.newInstance(requireActivity())
        }

        mViewModel.mUserProStudyTime.observe {
            it.doSuccess {
                srl.finishRefresh()
                tvStudyToday.text = it.todayStudyTime
                tvTotalToday.text = it.totalStudyTime
                tvIntegral.text = it.score
            }.ycShowNetError()
        }

        mViewModel.mUserProJoinInfo.observe { it ->
            it.doSuccess {
                tvPassAndAllExams.text = it.examPassNum + "/" + it.allExamNum + "场"
                tvParticipate.text = it.activeNum + "次"
            }.ycShowNetError()
        }
        mViewModel.mLogout.observe {
            it.doSuccess {
                LoginActivity.newInstance(requireContext())
                requireActivity().finish()
            }.ycShowNetError("退出登录失败")
        }
    }


}
